/*
 * @Description: 订单校验规则页面
 * @Version: 1.0
 * @Autor: c-jack.qian
 * @Date: 2020-10-21 17:30:52
 * @LastEditors: c-jack.qian
 * @LastEditTime: 2021-04-25 16:52:13
 */

import { renderSort } from '@src/utils/utilsFun';
import { Button, DataSet, Lov, notification, Table } from 'choerodon-ui/pro/lib';
import { ButtonColor, FuncType } from 'choerodon-ui/pro/lib/button/enum';
import { TableColumnTooltip } from 'choerodon-ui/pro/lib/table/enum';
import { Buttons } from 'choerodon-ui/pro/lib/table/Table';
import React, { useCallback, useMemo } from 'react';
import intl from 'utils/intl';
import { routerRedux } from 'dva/router';
import querystring from 'querystring';
import { connect } from 'dva';

notification.config({
  duration: 3,
  placement: 'bottomRight',
});
interface propstype {
  dispatch: any;
  tableDS: DataSet;
}

const ClassPage: React.FC<propstype> = ({ tableDS, dispatch }) => {
  const handleCreate = () => {
    tableDS.create({});
  };
  // 删除选择行
  const handleDelete = () => {
    const seletedRecord = tableDS.selected;
    if (seletedRecord.length === 0) {
      notification.warning({
        placement: 'bottomRight',
        message: intl.get(`dji.otc.om.view.reminder.warning`).d('提示'),
        description: intl.get(`dji.otc.om.view.reminder.selectDeleteData`).d('请勾选删除的数据'),
      });
      return;
    }
    tableDS.delete(seletedRecord);
  };
  const handleBatchExport = useCallback(() => {
    dispatch(
      routerRedux.push({
        pathname: `/o2om/distribution/excel-import/O2OM_C.QUOTA_PLAN`,
        search: querystring.stringify({
          action: intl.get(`dji.otc.om.view.button.distributionAdjustmentImport`).d('分货调整列表导入'),
          prefixPath: 'O2OM_C.QUOTA_PLAN',
        }),
      })
    );
  }, []);
  const Columns = useMemo<any>(
    () => [
      {
        header: intl.get(`dji.otc.om.view.field.serialNumber`).d('序号'),
        width: 50,
        lock: true,
        renderer: ({ record }) => renderSort(record, tableDS),
      },
      {
        name: 'productCodeObj',
        align: 'left',
        tooltip: TableColumnTooltip.always,
        lock: true,
        editor: ({}) => <Lov name="productCodeObj" />,
        width: 170,
      },
      {
        name: 'outAreaId',
        editor: true,
        align: 'left',
        tooltip: TableColumnTooltip.always,
        width: 170,
      },
      {
        name: 'outRetailer',
        editor: true,
        align: 'left',
        tooltip: TableColumnTooltip.always,
      },
      {
        name: 'outCustomerObj',
        align: 'left',
        tooltip: TableColumnTooltip.always,
        editor: ({}) => <Lov name="outCustomerObj" />,
      },
      {
        name: 'outWarehouse',
        editor: true,
        align: 'left',
        tooltip: TableColumnTooltip.always,
        width: 150,
      },
      {
        name: 'intoAreaId',
        editor: true,
        align: 'left',
        tooltip: TableColumnTooltip.always,
        width: 170,
      },
      {
        name: 'intoRetailer',
        editor: true,
        align: 'left',
        tooltip: TableColumnTooltip.always,
      },
      {
        name: 'intoCustomerObj',
        align: 'left',
        tooltip: TableColumnTooltip.always,
        editor: ({}) => <Lov name="intoCustomerObj" />,
      },
      {
        name: 'intoWarehouse',
        editor: true,
        align: 'left',
        tooltip: TableColumnTooltip.always,
        width: 150,
      },
      { name: 'quantity', editor: true, align: 'left', tooltip: TableColumnTooltip.always },
      { name: 'reason', editor: true, align: 'left', tooltip: TableColumnTooltip.always },
      { name: 'quantityAfterOut', editor: true, align: 'left', tooltip: TableColumnTooltip.always },
      {
        name: 'quantityAfterInto',
        editor: true,
        align: 'left',
        tooltip: TableColumnTooltip.always,
      },
    ],
    []
  );
  const buttons: Buttons[] = [
    <Button
      icon="add"
      key="create"
      color={ButtonColor.default}
      funcType={FuncType.raised}
      onClick={() => handleCreate()}
    >
      {intl.get(`dji.otc.om.view.button.create`).d('新建')}
    </Button>,
    <Button
      icon="delete"
      key="delete"
      onClick={() => handleDelete()}
      color={ButtonColor.default}
      funcType={FuncType.raised}
    >
      {intl.get(`dji.otc.om.view.button.delete`).d('删除')}
    </Button>,
    <Button
      icon="get_app"
      key="import"
      onClick={() => handleBatchExport()}
      color={ButtonColor.default}
      funcType={FuncType.raised}
    >
      {intl.get(`dji.otc.om.view.button.import`).d('导入')}
    </Button>,
  ];
  return (
    <Table
      style={{ margin: '20px 0' }}
      dataSet={tableDS}
      border
      queryFieldsLimit={3}
      columns={Columns}
      buttons={buttons}
    />
  );
};

export default connect()(ClassPage);
